package com.sds.sweng.swat.miplatform.service.graphics;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Node;
import org.eclipse.draw2d.IFigure;

import com.sds.sweng.swat.miplatform.service.figures.components.CalendarComponent;
import com.sds.sweng.swat.miplatform.service.figures.components.CheckboxComponent;
import com.sds.sweng.swat.miplatform.service.figures.components.ComboboxComponent;
import com.sds.sweng.swat.miplatform.service.figures.components.GridComponent;
import com.sds.sweng.swat.miplatform.service.figures.components.RadioComponent;
import com.sds.sweng.swat.miplatform.service.figures.components.SelectableComponent;
import com.sds.sweng.swat.miplatform.service.figures.components.SimpleComponent;
import com.sds.sweng.swat.miplatform.service.figures.components.TabComponent;
import com.sds.sweng.swat.miplatform.service.figures.components.TextboxComponent;
import com.sds.sweng.swat.miplatform.service.pages.ServiceDesignArea;

public class ComponentDrawer {

	private IFigure rootFigure;

	public ServiceDesignArea designArea;

	public ComponentDrawer(IFigure rootFigure, ServiceDesignArea designArea) {
		this.rootFigure = rootFigure;
		this.designArea = designArea;
	}

	public void drawAllComponents(Node parent) {
		drawComponents("Static", parent);
		drawComponents("List", parent);
		drawComponents("TreeView", parent);

		drawComponents("Button", parent);
		drawComponents("Edit", parent);
		drawComponents("MaskEdit", parent);
		drawComponents("Spin", parent);
		drawComponents("TextArea", parent);
		drawComponents("Calendar2", parent);

		drawComponents("Combo", parent);
		drawComponents("Checkbox", parent);
		drawComponents("Radio", parent);

		drawComponents("Grid", parent);

		drawComponents("Div", parent);

		drawComponents("Tab", parent);
	}

	private void drawComponents(String componetName, Node parent) {
		List<Node> nodes = parent.selectNodes(componetName);

		boolean hasBorder = false;

		if ("Button".equals(componetName)) {
			hasBorder = true;
		}

		if ("Grid".equals(componetName)) {
			for (Node node : nodes) {
				drawGridComponent(node, parent);
			}
		} else if ("Div".equals(componetName)) {
			for (Node node : nodes) {
				Node divNode = node.selectSingleNode("Contents");

				drawAllComponents(divNode);
			}
		} else if ("Tab".equals(componetName)) {
			for (Node node : nodes) {
				drawTabComponent(node, parent);
			}
		} else if ("Edit".equals(componetName)) {
			for (Node node : nodes) {
				drawTextboxComponent(node, true, parent);
			}
		} else if ("MaskEdit".equals(componetName)) {
			for (Node node : nodes) {
				drawTextboxComponent(node, true, parent);
			}
		} else if ("Spin".equals(componetName)) {
			for (Node node : nodes) {
				drawTextboxComponent(node, true, parent);
			}
		} else if ("TextArea".equals(componetName)) {
			for (Node node : nodes) {
				drawTextboxComponent(node, true, parent);
			}
		} else if ("Calendar2".equals(componetName)) {
			for (Node node : nodes) {
				drawCalendarComponent(node, parent);
			}
		} else if ("Combo".equals(componetName)) {
			for (Node node : nodes) {
				drawComboboxComponent(node, parent);
			}
		} else if ("Checkbox".equals(componetName)) {
			for (Node node : nodes) {
				drawCheckboxComponent(node, parent);
			}
		} else if ("Radio".equals(componetName)) {
			for (Node node : nodes) {
				drawRadioComponent(node, parent);
			}
		} else if ("TreeView".equals(componetName)) {
			for (Node node : nodes) {
				drawSimpleComponent(node, true, parent);
			}
		} else {
			for (Node node : nodes) {
				drawSimpleComponent(node, hasBorder, parent);
			}
		}
	}

	private void drawSimpleComponent(Node component, boolean hasBorder, Node parent) {
		SimpleComponent simpleComponent = new SimpleComponent(component, parent);

		if (!hasBorder) {
			simpleComponent.hasBorder(StringUtils.isEmpty(component.valueOf("@Border")) ? false : true);
		}

		rootFigure.getLayoutManager().setConstraint(simpleComponent, simpleComponent.getBounds());
		rootFigure.add(simpleComponent);
	}

	private void drawTextboxComponent(Node component, boolean hasBorder, Node parent) {
		TextboxComponent textboxComponent = new TextboxComponent(component, parent);
		textboxComponent.setDrawer(this);

		if (!hasBorder) {
			textboxComponent.hasBorder(StringUtils.isEmpty(component.valueOf("@Border")) ? false : true);
		}

		rootFigure.getLayoutManager().setConstraint(textboxComponent, textboxComponent.getBounds());
		rootFigure.add(textboxComponent);

		designArea.addValidationComponent(textboxComponent.getId(), textboxComponent);
	}

	private void drawCalendarComponent(Node component, Node parent) {
		CalendarComponent calendarComponent = new CalendarComponent(component, parent);
		calendarComponent.setDrawer(this);

		rootFigure.getLayoutManager().setConstraint(calendarComponent, calendarComponent.getBounds());
		rootFigure.add(calendarComponent);

		designArea.addValidationComponent(calendarComponent.getId(), calendarComponent);
	}

	private void drawComboboxComponent(Node component, Node parent) {
		ComboboxComponent comboboxComponent = new ComboboxComponent(component, parent);
		comboboxComponent.setDrawer(this);

		rootFigure.getLayoutManager().setConstraint(comboboxComponent, comboboxComponent.getBounds());
		rootFigure.add(comboboxComponent);

		designArea.addValidationComponent(comboboxComponent.getId(), comboboxComponent);
	}

	private void drawCheckboxComponent(Node component, Node parent) {
		CheckboxComponent checkboxComponent = new CheckboxComponent(component, parent);
		checkboxComponent.setDrawer(this);

		rootFigure.getLayoutManager().setConstraint(checkboxComponent, checkboxComponent.getBounds());
		rootFigure.add(checkboxComponent);

		designArea.addValidationComponent(checkboxComponent.getId(), checkboxComponent);
	}

	private void drawRadioComponent(Node component, Node parent) {
		RadioComponent radioComponent = new RadioComponent(component, parent);
		radioComponent.setDrawer(this);

		rootFigure.getLayoutManager().setConstraint(radioComponent, radioComponent.getBounds());
		rootFigure.add(radioComponent);

		designArea.addValidationComponent(radioComponent.getId(), radioComponent);
	}

	private void drawGridComponent(Node grid, Node parent) {
		GridComponent gridComponent = new GridComponent(grid, parent, this);

		rootFigure.getLayoutManager().setConstraint(gridComponent, gridComponent.getBounds());
		rootFigure.add(gridComponent);
	}

	private void drawTabComponent(Node tab, Node parent) {
		TabComponent tabComponent = new TabComponent(tab, parent, this);

		rootFigure.getLayoutManager().setConstraint(tabComponent, tabComponent.getBounds());
		rootFigure.add(tabComponent);
	}

	public void setSelected(SelectableComponent selectedComponent) {
		if (selectedComponent.equals(designArea.currentSelectedComponent)) {
			return;
		}

		designArea.previosSelectedComponent = designArea.currentSelectedComponent;
		designArea.currentSelectedComponent = selectedComponent;

		if (designArea.previosSelectedComponent != null) {
			designArea.previosSelectedComponent.setSelected(false);
		}

		designArea.currentSelectedComponent.setSelected(true);

		designArea.setSelected(selectedComponent);
	}

}
